//
//  YXNetworkTool.swift
//  微博YX
//
//  Created by apple on 15/11/25.
//  Copyright © 2015年 lyx. All rights reserved.
//

import UIKit
import AFNetworking
/// 管理
enum YXNetworkToolError: Int {
    case EmptyAccessToken = -1
    case EmptyUid = -2
    
    
    var description: String {
        switch self {
        case .EmptyAccessToken:
            return "AccessToken 没有值"
        case .EmptyUid:
            return "Uid 没有值"
        }
    }
    
    // 根据当前类型返回一个NSError
    func error() -> NSError {
        return NSError(domain: "cn.itcast.error.net", code: self.rawValue, userInfo: ["description" : self.description])
    }
}

class YXNetworkTool: AFHTTPSessionManager {
// 单例
    static let sharedInstance : YXNetworkTool = {
        let baseURL = NSURL(string: "https://api.weibo.com/")
        let tool = YXNetworkTool(baseURL: baseURL)
//        添加序列化器
        tool.responseSerializer.acceptableContentTypes?.insert("text/plain")
        return tool
    }()
 
    // MARK: - Oauth授权
    /// 申请应用时分配的AppKey
    private let client_id = "1333947267"
    
    /// 申请应用时分配的AppSecret
    private let client_secret = "1028eae8419b944b34a95eda301b30f1"
    
    /// 授权回调地址, 一定要一模一样
    let redirect_uri = "http://www.baidu.com/"
    
//    返回授权页面的url地址
    func oauthURL() ->NSURL {
        let urlString = "https://api.weibo.com/oauth2/authorize?client_id=\(client_id)&redirect_uri=\(redirect_uri)"
        return NSURL(string: urlString)!
    }
    
//    MARK : - 加载token   只赋值加载数据
    func loadAccessToken(code: String ,finished: (result: [String: AnyObject]?, error: NSError?) -> ()) {
        let urlString = "https://api.weibo.com/oauth2/access_token"
        let parametes = [
            "client_id": client_id,
            "client_secret": client_secret,
            "grant_type": "authorization_code",
            "code": code,
            "redirect_uri": redirect_uri
        ]
        POST(urlString, parameters: parametes, success: { (_, result) -> Void in
//            成功通知回调
            finished(result: result as? [String : AnyObject] , error: nil)
            }) { (_, error) -> Void in
//                失败通知回调
            finished(result:nil , error: error)
        }
    }
    
    // MARK: - 获取用户数据
    func loadUserInfo(finished: (result: [String: AnyObject]?, error: NSError?) -> ()) {
        guard let access_token = YXUserAccount.loadUserAccount()?.access_token else {
            // 通知调用者,
            let error = YXNetworkToolError.EmptyAccessToken.error()
            finished(result: nil, error: error)
            return
        }
        
    
        guard let uid = YXUserAccount.loadUserAccount()?.uid else {
            // 通知调用者
            let error = YXNetworkToolError.EmptyUid.error()
            finished(result: nil, error: error)
            return
        }
        let urlString = "2/users/show.json"
        // 参数
        let parameters = [
            "access_token": access_token,
            "uid": uid
        ]
        // 发送请求
        GET(urlString, parameters: parameters, success: { (_, result) -> Void in
            finished(result: result as? [String: AnyObject], error: nil)
            }) { (_, error) -> Void in
                finished(result: nil, error: error)
        }
    }
    
    /// MARK: 加载微博数据
    func loadStatus (finished: (result: [String: AnyObject]?, error: NSError?) -> ()){
        guard let access_token = YXUserAccount.loadUserAccount()?.access_token else {
          print("access_token没有值")
            return
        }
        
        guard let uid = YXUserAccount.loadUserAccount()?.uid else {
          print("uid 没有值")
            return
        }
        
        
//        url
       let urlString = "https://api.weibo.com/2/statuses/home_timeline.json"
        
        
//        参数
       let parameters = [
         "access_token": access_token,
         "uid" : uid
        ]
        
//        发送请求
        GET(urlString, parameters: parameters, success: { (_, result) -> Void in
            finished(result: result as? [String: AnyObject], error: nil)
            }) { (_, error) -> Void in
          finished(result: nil, error: error)
        }
    }

}
